﻿
@inject IHttpService _httpService

@inject ILocalStorageService _localStorage
@inject IPopupService PopupService


@inject NavigationManager NavigationManager
@inject IPageModelCatche<List<EditInformationModel>> _pageModelCatche


<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="创建词条"></CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip>

<AutoSaveTip TItem="EstablishEntryViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />
<div>
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.EditSummaryTip Type="ExaminedNormalListModelType.Entry" Class="mb-3" Creating />
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        @if (Model.Type == EntryType.Staff)
        {
            <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Error" Class="mb-3">
                这个词条可能涉及到现实人物，请勿过分较真，也请谨慎编辑，仔细斟酌词句
            </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>
        }
        @if (Model.Type == EntryType.Game)
        {
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.SynchronousSteamInforCard AddInforModel="Model.AddInfor" ImagesModel="Model.Images" MainModel="Model.Main" MainPageModel="Model.MainPage" OnSynchronized="OnSynchronized" />
        }

        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="创建词条" Icon="@IconType.Entry.ToIconString()" Outline>

            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.MainCard Model="Model.Main" Class="mb-4" TypeChanged="TypeChanged" ExistingItems="EntryItems" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.AddInforCard Model="Model.AddInfor" EntryGroupItems="EntryGroupItems" EntryStaffItems="EntryStaffItems" LotteryItems="LotteryItems" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.ImagesCard Model="Model.Images" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.AudioCard Model="Model.Audio" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.TagsCard Model="Model.Tags" TagItems="TagItems" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.RelevancesCard Model="Model.Relevances" VideoItems="VideoItems" EntryGameItems="EntryGameItems" EntryRoleItems="EntryRoleItems" EntryGroupItems="EntryGroupItems" EntryStaffItems="EntryStaffItems" ArticleItems="ArticleItems" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.MainPageCard Model="Model.MainPage" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.WebsiteCard Model="Model.Website" Class="mb-4" />

            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    }
</div>
@code {
    [Parameter]
    [SupplyParameterFromQuery]
    public string Name { get; set; }

    private EstablishEntryViewModel Model { get; set; } = new EstablishEntryViewModel();



    private bool isReady = false;

    private List<string> EntryGameItems { get; set; } = new List<string>();
    private List<string> EntryRoleItems { get; set; } = new List<string>();
    private List<string> EntryGroupItems { get; set; } = new List<string>();
    private List<string> EntryStaffItems { get; set; } = new List<string>();
    private List<string> EntryItems { get; set; } = new List<string>();
    private List<string> ArticleItems { get; set; } = new List<string>();
    private List<string> TagItems { get; set; } = new List<string>();
    private List<string> VideoItems { get; set; } = new List<string>();
    private List<string> LotteryItems { get; set; } = new List<string>();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    AutoSaveTip<EstablishEntryViewModel> autoSave { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                await GetInformation();
                Model.Main.Name = Model.Main.DisplayName = Name;
                //获取所有词条
                EntryGameItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/0");
                EntryRoleItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/1");
                EntryGroupItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/2");
                EntryStaffItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/3");
                EntryItems.AddRange(EntryGameItems);
                EntryItems.AddRange(EntryRoleItems);
                EntryItems.AddRange(EntryGroupItems);
                EntryItems.AddRange(EntryStaffItems);
                //获取所有文章
                ArticleItems = await _httpService.GetAsync<List<string>>("api/articles/GetAllArticles");
                //获取所有标签
                TagItems = await _httpService.GetAsync<List<string>>("api/tags/GetAllTags");
                //获取所有视频
                VideoItems = await _httpService.GetAsync<List<string>>("api/videos/GetNames");
                //获取所有抽奖
                LotteryItems = await _httpService.GetAsync<List<string>>("api/lotteries/GetNames");
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取所有词条文章标签名称列表失败");
            }
        }
    }

    public async Task GetInformation()
    {
        Model.AddInfor.Informations = await _pageModelCatche.GetCache($"api/entries/GetEditInformationModelList?type={(int)Model.Type}");
        foreach(var item in Model.AddInfor.Informations)
        {
            item.Value = null;
        }
    }


    private async Task OnSubmit()
    {
        try
        {

            var obj = await _httpService.PostAsync<EstablishEntryViewModel, Result>("api/entries/EstablishEntry", Model);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("创建词条失败", obj.Error);
            }
            else
            {
                await autoSave.Clean();

                await PopupService.ToastSuccessAsync("创建词条成功", "创建词条成功，等待审核通过");
                NavigationManager.NavigateTo("/entries/index/" + obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "创建词条失败");
        }
    }

    public void OnSynchronized()
    {
        StateHasChanged();
    }

    public async Task TypeChanged(EntryType type)
    {
        Model.Type= Model.AddInfor.Type = Model.Relevances.Type = type;
        await GetInformation();
        StateHasChanged();

    }

    private void DataLoaded(EstablishEntryViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();

    }

}
